﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Nextensions.GUI
{
    public class SelectableListWindow: Form
    {
        private ListBox _list;
        private string[] _itemCache;
        private string _startsWithFilter = "";

        public SelectableListWindow()
        {
            _list = new ListBox();
            _list.Dock = DockStyle.Fill;
            _list.KeyUp += new KeyEventHandler(_list_KeyUp);
            this.Controls.Add(_list);

            MaximizeBox = false;
            MinimizeBox = false;
            ShowIcon = false;
            ShowInTaskbar = false;
            FormBorderStyle = FormBorderStyle.None;
        }

        public string Path
        {
            get
            {
                return _list.SelectedItem == null ? "" : _list.SelectedItem.ToString();
            }
        }

        private void _list_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                Hide();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                _list.SelectedValue = null;
                Hide();
            }
        }

        protected override void OnActivated(System.EventArgs e)
        {
            base.OnActivated(e);

            //if (_list.Items.Count > 0)
            //    _list.SelectedIndex = 0;
        }

        protected override void OnDeactivate(System.EventArgs e)
        {
            base.OnDeactivate(e);
        }

        protected override void OnLostFocus(System.EventArgs e)
        {
            base.OnLostFocus(e);
        }

        protected override bool ShowWithoutActivation
        {
            get
            {
                return true;
            }
        }

        /*public void AddItem(string Item)
        {
            _list.Items.Add(Item);
        }*/

        public void SetItems(string[] Items)
        {
            _list.Items.Clear();
            _list.Items.AddRange(Items);
            _itemCache = Items;
        }

        public void SetStartsWithFilter(string StartsWith)
        {
            int len = StartsWith.Length;

            if (len < _startsWithFilter.Length)
            {
                _list.Items.Clear();
                _list.Items.AddRange(_itemCache);
            }

            for (int i = 0; i < _list.Items.Count; i++)
            {
                string item = _list.Items[i].ToString();

                if (String.Compare(item.Substring(0, len), StartsWith) != 0)
                {
                    _list.Items.RemoveAt(i);
                    i--;
                }
            }

            _startsWithFilter = StartsWith;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // SelectableListWindow
            // 
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "SelectableListWindow";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.ResumeLayout(false);
        }
    }
}
